package com.object;
import java.util.ArrayList;
import java.util.Scanner;
public class ShoppingCart extends Pet{

    private String state = "失效";

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public User addInShopping(User user, ArrayList<OperateLog>operateLogs, Pet pet, Scanner scanner){
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setId(pet.getId());
        shoppingCart.setName(pet.getName());
        shoppingCart.setGender(pet.getGender());
        shoppingCart.setAge(pet.getAge());
        shoppingCart.setKind(pet.getKind());
        shoppingCart.setColor(pet.getColor());
        shoppingCart.setPrice(pet.getPrice());
        user.getShoppingCarts().add(shoppingCart);
        System.out.println("已将"+pet.getName()+"添加至用户:"+user.getName()+"的购物车");
        return user;
    }
    //查看已在购物车内的商品是否失效
    public void islapse(User user,ArrayList<Pet>pets){
        for (ShoppingCart shoppingCart : user.getShoppingCarts()) {
            for (Pet pet : pets) {
                if(shoppingCart.getId().equals(pet.getId())){
                    //说明在宠物里面找不到我的购物车里面的id，说明该宠物已经被购买了
                    shoppingCart.setState("正常");
                    break;
                }
            }
        }
    }
    //如果失效提示删除
//    public void islapsed(User user,Scanner scanner){
//        for (ShoppingCart shoppingCart : user.getShoppingCarts()) {
//            if(shoppingCart.getState().equals("失效")){
//                System.out.println("是否删除 1:删除   其他:不删除");
//                if(scanner.next().equals("1")){
//
//                }
//            }
//        }
//    }
    public void showShopping(ArrayList<OperateLog> operateLogs, User user,Scanner scanner,ArrayList<Pet>pets) {
        islapse(user,pets);
        System.out.println("ID    昵称    性别    年龄    品种    颜色    价格    购物车状态");
        String type = "查看购物车";
        for (ShoppingCart shoppingCart : user.getShoppingCarts()) {
            System.out.println(shoppingCart.getId() + "  " + shoppingCart.getName() + " " +
                    "   " + shoppingCart.getGender() + "    " + shoppingCart.getAge() + " " +
                    "    " + shoppingCart.getKind() + "  " + shoppingCart.getColor() + "  " +
                    "  " + shoppingCart.getPrice()+"   "+shoppingCart.getState());
        }
//        islapsed(user,scanner);
        new OperateLog().addLog(operateLogs, user, type);
    }
    public User chooseShopping(ArrayList<OperateLog> operateLogs, User user,Scanner scanner,ArrayList<Pet> pets){
        if(user.getShoppingCarts().isEmpty()){
            System.out.println("您的购物车内没有商品");
            return  user;
        }
        System.out.println("请选择:   1:选择结算   2:移除    其他:啥也不做，返回上一步");
        String choose = scanner.next();
        if(choose.equals("1")){
            //选择商品购买
            buy(operateLogs,user,scanner,1,pets);
        }else if (choose.equals("2")){
            //根据id删除购物车
            remove(operateLogs,user,scanner);
        }else {
            System.out.println("已返回");
        }
        return user;
    }
    public void  payFor(ArrayList<OperateLog> operateLogs, User user,Scanner scanner,ArrayList<Pet>pets){
        System.out.println("共计:"+user.getOrders().get(0).getPrice());
        System.out.println("1：立即支付    其他：稍后支付" );
        if(scanner.next().equals("1")){
            user.getOrders().get(0).setState("已支付");
            removeCart(user,scanner);//只要支付自动删除购物车对应商品
            pets = new Pet().removePet(user, pets);//todo 也要删除宠物列表中的对应宠物
        }else {
            user.getOrders().get(0).setState("待支付");
        }
    }
    public  void  removeCart(User user,Scanner scanner){
        for (OrderList orderList : user.getOrders().get(0).getOrderLists()) {
            for (ShoppingCart shoppingCart : user.getShoppingCarts()){
                if (orderList.getId().equals(shoppingCart.getId())){
                    user.getShoppingCarts().remove(shoppingCart);
                    break;
                }
            }
        }
    }
    //判断商品在不在订单里面
    public boolean isIn(User user,String id){
        for (OrderList orderList : user.getOrders().get(0).getOrderLists()) {
            if(id.equals(orderList.getId())){
                return true;
            }
        }
        return false;
    }
    public  User buy(ArrayList<OperateLog> operateLogs, User user,Scanner scanner,int i,ArrayList<Pet> pets){

        System.out.println("请选择要购买的商品id--->输入1取消");
        String id = scanner.next();
        if(id.equals("1")){
            user.getOrders().get(0).setState("未支付");
            return user;
        }
        boolean isHad= idIsHad(id,user);
        if (!(user.getOrders().isEmpty())){
            if(isIn(user, id)){
                System.out.println("您已添加过，请重新选择");
                buy(operateLogs,user,scanner,i,pets);
            }
        }
        if(isHad){
           if(i ==1){
               newOrder(operateLogs,user,scanner,id);
               System.out.println("是否继续结算？  1:继续  2:取消");
           }else {
                addOrder(operateLogs,user,scanner,id);
               System.out.println("是否继续继续？  1:继续  2:取消");
           }
            if(scanner.next().equals("1")){
                buy(operateLogs,user,scanner,2,pets);
            }else {
                payFor(operateLogs,user,scanner,pets);
            }
        }else {
            System.out.println("id不存在");
            buy(operateLogs,user,scanner,1,pets);
        }
        return  user;
    }
    public  User newOrder(ArrayList<OperateLog> operateLogs, User user,Scanner scanner,String id){
        OrderList orderList = new OrderList();
        Order order = new Order();
        for (ShoppingCart shoppingCart : user.getShoppingCarts()) {
            if (shoppingCart.getId().equals(id)){
                newOrderList(orderList, shoppingCart);
                order.setId(user.newUid(8));
                order.setTime(user.getTime());
                order.setPrice(order.getPrice()+orderList.getPrice());
                order.getOrderLists().add(orderList);
                user.getOrders().add(0,order);
                PetObj petObj = new PetObj();
                petObj.setOrders(user.getOrders());
                user.petObj=petObj;
                break;
            }
        }
        return user;
    }
    public  User addOrder(ArrayList<OperateLog> operateLogs, User user,Scanner scanner,String id){
        OrderList orderList = new OrderList();
        for (ShoppingCart shoppingCart : user.getShoppingCarts()) {
            if (shoppingCart.getId().equals(id)){
                newOrderList(orderList, shoppingCart);
                ArrayList<OrderList> orderLists = user.getOrders().get(0).getOrderLists();
                orderLists.add(orderList);
                user.getOrders().get(0).setPrice(user.getOrders().get(0).getPrice()+orderList.getPrice());
                user.getOrders().get(0).setOrderLists(orderLists);
                PetObj petObj = new PetObj();
                petObj.setOrders(user.getOrders());
                user.petObj=petObj;
                break;
            }
        }
        return user;
    }

    private void newOrderList(OrderList orderList, ShoppingCart shoppingCart) {
        orderList.setId(shoppingCart.getId());
        orderList.setName(shoppingCart.getName());
        orderList.setGender(shoppingCart.getGender());
        orderList.setAge(shoppingCart.getAge());
        orderList.setKind(shoppingCart.getKind());
        orderList.setColor(shoppingCart.getColor());
        orderList.setPrice(shoppingCart.getPrice());
    }

    //判断id在不在购物车里面
    public  boolean idIsHad(String id,User user){
        boolean isFind = false;
        for (ShoppingCart cart : user.getShoppingCarts()) {
            if(id.equals(cart.getId())){
                isFind = true;
                break;
            }
        }
        return isFind;
    }
    public  User remove(ArrayList<OperateLog> operateLogs, User user,Scanner scanner){
        System.out.println("请选择要移除的宠物id");
        ShoppingCart shoppingCart = new ShoppingCart();
        String id = scanner.next();
        boolean isFind = idIsHad(id,user);
        String type;
        if(isFind){
            System.out.println("确认要移除吗？1:确认    2:取消");
            if(scanner.next().equals("1")){
                for (ShoppingCart cart : user.getShoppingCarts()) {
                    if(id.equals(cart.getId())){
                        user.getShoppingCarts().remove(cart);
                        type = "将"+cart.getName()+"从购物车移除了";
                        new OperateLog().addLog(operateLogs,user,type);
                        break;
                    }
                }
            }else{
                type = "取消了移除"+shoppingCart.getName();
                System.out.println("用户取消了移除"+shoppingCart.getName());
                new OperateLog().addLog(operateLogs,user,type);
            }
        }else {
            System.out.println("id不存在请重新输入");
            remove(operateLogs,user,scanner);
        }
        return user;
    }
}
